package com.neiquan.weiguan.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.neiquan.weiguan.R;
import com.neiquan.weiguan.activity.ContainerActivity;
import com.neiquan.weiguan.colorful.Colorful;
import com.neiquan.weiguan.fragment.base.AppBaseFragment;
import com.neiquan.weiguan.utils.Constant;
import com.neiquan.weiguan.widget.pulltozoomview.PullToZoomScrollViewEx;

import org.haitao.common.utils.PixelUtil;
import org.haitao.common.utils.SharedPreferencesUtil;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by cxh on 2016/6/2.
 */
public class MeFragment extends AppBaseFragment implements View.OnClickListener {

    @InjectView(R.id.pulltozoomscrollview)
    PullToZoomScrollViewEx mPulltozoomscrollview;
    private TextView text_night;
    private boolean isday;

    @Override
    public View initRootView() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_me, null);
        ButterKnife.inject(this, view);
        return view;
    }

    @Override
    public void setViews() {
        View headlayout = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_me_headlayout, null);
        View zoomlayout = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_me_zoomlayout, null);
        View contentlayout = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_me_contentlayout, null);
        mPulltozoomscrollview.setZoomView(zoomlayout);
        mPulltozoomscrollview.setHeaderView(headlayout);
        mPulltozoomscrollview.setScrollContentView(contentlayout);
        int mScreenWidth = PixelUtil.getScreenWidth(getActivity());
        LinearLayout.LayoutParams localObject = new LinearLayout.LayoutParams(mScreenWidth, (int) (9.0F * (mScreenWidth / 16.0F)));
        mPulltozoomscrollview.setHeaderLayoutParams(localObject);
        text_night = (TextView) mPulltozoomscrollview.getPullRootView().findViewById(R.id.text_night);
        mPulltozoomscrollview.getPullRootView().findViewById(R.id.lin_loginmode).setVisibility(View.VISIBLE);
        mPulltozoomscrollview.getPullRootView().findViewById(R.id.lin_user).setVisibility(View.GONE);
        mPulltozoomscrollview.getPullRootView().findViewById(R.id.lin_night).setOnClickListener(this);
        mPulltozoomscrollview.getPullRootView().findViewById(R.id.lin_collection).setOnClickListener(this);
        mPulltozoomscrollview.getPullRootView().findViewById(R.id.lin_setup).setOnClickListener(this);
        mPulltozoomscrollview.getPullRootView().findViewById(R.id.lin_one_Item).setOnClickListener(this);
        mPulltozoomscrollview.getPullRootView().findViewById(R.id.lin_two_Item).setOnClickListener(this);
        mPulltozoomscrollview.getPullRootView().findViewById(R.id.lin_three_Item).setOnClickListener(this);
        mPulltozoomscrollview.getPullRootView().findViewById(R.id.lin_four_Item).setOnClickListener(this);
        mPulltozoomscrollview.getPullRootView().findViewById(R.id.lin_five_Item).setOnClickListener(this);
        mPulltozoomscrollview.getPullRootView().findViewById(R.id.lin_six_Item).setOnClickListener(this);
        mPulltozoomscrollview.getPullRootView().findViewById(R.id.text_exchange).setOnClickListener(this);
        // 初始化Colorful
        setupColorful();
    }

    @Override
    public void initData() {
        isday = SharedPreferencesUtil.getBoolean(getContext(), Constant.SHAREDPREFERENCES_ISDAY);
        setText_nightText();
    }

    private void setText_nightText() {
        if (isday) {
            text_night.setText("日间");
        } else {
            text_night.setText("夜间");
        }
    }

    /**
     * 设置各个视图与颜色属性的关联
     */
    protected void setupColorful() {
        //ViewGroupSetter listViewSetter = new ViewGroupSetter(mNewsListView);
        // 绑定ListView的Item View中的news_title视图，在换肤时修改它的text_color属性
        //listViewSetter.childViewTextColor(R.id.news_title, R.attr.text_color);
        // 构建Colorful对象来绑定View与属性的对象关系
        colorful = new Colorful.Builder(this)
                .backgroundColor(R.id.pulltozoomscrollview, R.attr.root_view_bg)
                .backgroundDrawable(R.id.root_view, R.attr.root_view_bg)
                        // 设置view的背景图片
                .backgroundColor(R.id.lin_collection, R.attr.root_view_bg)
                .backgroundDrawable(R.id.img_collection, R.attr.collection_drawable)
                .backgroundColor(R.id.lin_night, R.attr.root_view_bg)
                .backgroundDrawable(R.id.img_night, R.attr.collection_drawable)
                .backgroundColor(R.id.lin_setup, R.attr.root_view_bg)
                .backgroundDrawable(R.id.img_setup, R.attr.collection_drawable)
                .backgroundColor(R.id.lin_one_Item, R.attr.root_view_bg)
                .textColor(R.id.text_one_Item, R.attr.text_color)
                .backgroundColor(R.id.lin_two_Item, R.attr.root_view_bg)
                .textColor(R.id.text_two_Item, R.attr.text_color)
                .backgroundColor(R.id.lin_three_Item, R.attr.root_view_bg)
                .textColor(R.id.text_three_Item, R.attr.text_color)
                .backgroundColor(R.id.lin_four_Item, R.attr.root_view_bg)
                .textColor(R.id.text_four_Item, R.attr.text_color)
                .textColor(R.id.text_collection, R.attr.text_color)
                .textColor(R.id.text_night, R.attr.text_color)
                .backgroundColor(R.id.lin_me_fragment_bg, R.attr.root_view_bg)
                        // 设置背景色
                .textColor(R.id.text_setup, R.attr.text_color)
//                .setter(listViewSetter) // 手动设置setter
                .create(); // 设置文本颜色
    }

    /**
     * 切换主题
     */
    private void changeThemeWithColorful() {
        this.isday = !isday;
        setText_nightText();
        SharedPreferencesUtil.add(getContext(), Constant.SHAREDPREFERENCES_ISDAY, isday);
        getActivity().sendBroadcast(new Intent(Constant.THEMECHANGE_BROADCAST));
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.lin_night:
                changeThemeWithColorful();
                break;
            case R.id.lin_collection:
                ContainerActivity.startActivity(getContext(), CollectionFragment.class, null);
                break;
            case R.id.lin_setup:
                ContainerActivity.startActivity(getContext(), SetUpFragment.class, null);
                break;
            case R.id.lin_one_Item:
                ContainerActivity.startActivity(getContext(), MessageNotFragment.class, null);
                break;
            case R.id.lin_two_Item:

                break;
            case R.id.lin_three_Item:
                ContainerActivity.startActivity(getContext(), TipOffFragment.class, null);
                break;
            case R.id.lin_four_Item:
                ContainerActivity.startActivity(getContext(), FeedbackFragment.class, null);
                break;
            case R.id.lin_five_Item:

                break;
            case R.id.lin_six_Item:

                break;
            case R.id.text_exchange:
                ContainerActivity.startActivity(getContext(), ExchangeGiftFragment.class, null);
                break;
        }
    }
}
